package wpmp.auth.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wcommons.httputils.ResponseUtils;
import wcommons.httputils.quickbean.RespInfo;
import wpmp.auth.service.KeyValueService;
import wpmp.utils.quickbean.KeyValue;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:59:34 PM Mar 25, 2013
 */
@Controller
@RequestMapping("/keyValue.do")
public class KeyValueController {

	private static final Logger LOG = LoggerFactory.getLogger(KeyValueController.class);

	private KeyValueService service;

	@Autowired
	public void setService(KeyValueService service) {
		this.service = service;
	}

	@RequestMapping(params = "action=list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(HttpServletRequest req, HttpServletResponse resp) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to get all the KeyValue", req.getRemoteAddr());
		}

		final List<KeyValue> results = service.queryForList();

		ResponseUtils.renderJson(resp, new RespInfo<List<KeyValue>>(results).toString());
	}

	@RequestMapping(params = "action=refresh", method = { RequestMethod.GET, RequestMethod.POST })
	public void refresh(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "key", required = true) String key) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("[{}] access to refresh KeyValue[{}]", req.getRemoteAddr(), key);
		}
	}
}
